<template>
  <div class="coupon-management">
    <!-- 操作按钮 -->
    <el-button type="primary" icon="el-icon-plus" @click="openAddDialog">新增优惠券</el-button>

    <!-- 优惠券信息表格 -->
    <el-table :data="paginatedCoupons" style="width: 100%" stripe>
      <el-table-column label="优惠券编号" prop="id" width="150" />
      <el-table-column label="优惠券类型" prop="coupons_type" width="200" />
      <el-table-column label="减免金额" prop="amount" width="150" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="text" size="small" @click="openEditDialog(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteCoupon(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控制和每页条数选择 -->
    <div class="pagination-wrapper">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :total="coupons.length"
          layout="total, prev, pager, next, jumper"
          @current-change="handlePageChange"
          class="pagination"
      />
      <el-select v-model="pageSize" class="page-size-select" @change="handlePageSizeChange" placeholder="每页显示条数">
        <el-option label="3条" :value="3"></el-option>
        <el-option label="5条" :value="5"></el-option>
        <el-option label="10条" :value="10"></el-option>
        <el-option label="20条" :value="20"></el-option>
      </el-select>
    </div>

    <!-- 新增/编辑优惠券弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="currentCoupon" :rules="rules" ref="couponForm" label-width="100px">
        <el-form-item label="优惠券编号" prop="id">
          <el-input v-model="currentCoupon.id" disabled placeholder="自动生成" />
        </el-form-item>
        <el-form-item label="优惠券类型" prop="coupons_type">
          <el-select v-model="currentCoupon.coupons_type" placeholder="请选择优惠券类型">
            <el-option label="减免" value="subtract"></el-option>
            <!-- 可扩展其他优惠券类型 -->
          </el-select>
        </el-form-item>
        <el-form-item label="减免金额" prop="amount">
          <el-input v-model="currentCoupon.amount" placeholder="请输入减免金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCoupon">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      coupons: [
        { id: 1, coupons_type: 'subtract', amount: 50 },
        { id: 2, coupons_type: 'subtract', amount: 100 },
        { id: 3, coupons_type: 'subtract', amount: 30 }
      ],
      currentCoupon: { id: null, coupons_type: '', amount: '' },
      dialogVisible: false,
      dialogTitle: '新增优惠券',
      currentPage: 1,
      pageSize: 3,
      rules: {
        coupons_type: [{ required: true, message: '请选择优惠券类型', trigger: 'change' }],
        amount: [{ required: true, message: '请输入减免金额', trigger: 'blur' }]
      }
    };
  },
  computed: {
    paginatedCoupons() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = this.currentPage * this.pageSize;
      return this.coupons.slice(start, end);
    }
  },
  methods: {
    openAddDialog() {
      this.currentCoupon = { id: null, coupons_type: '', amount: '' };
      this.dialogTitle = '新增优惠券';
      this.dialogVisible = true;
    },
    openEditDialog(coupon) {
      this.currentCoupon = { ...coupon };
      this.dialogTitle = '编辑优惠券';
      this.dialogVisible = true;
    },
    saveCoupon() {
      this.$refs.couponForm.validate((valid) => {
        if (valid) {
          if (this.currentCoupon.id) {
            const index = this.coupons.findIndex(coupon => coupon.id === this.currentCoupon.id);
            this.$set(this.coupons, index, this.currentCoupon);
          } else {
            this.currentCoupon.id = this.coupons.length + 1;
            this.coupons.push(this.currentCoupon);
          }
          this.dialogVisible = false;
          this.$message.success(`${this.dialogTitle === '新增优惠券' ? '新增' : '编辑'}成功`);
        }
      });
    },
    deleteCoupon(coupon) {
      this.$confirm('确认删除该优惠券吗?', '删除优惠券', { type: 'warning' }).then(() => {
        this.coupons = this.coupons.filter(item => item.id !== coupon.id);
        this.$message.success('删除成功');
      }).catch(() => {});
    },

    handlePageChange(page) {
      this.currentPage = page;
    },

    handlePageSizeChange(size) {
      this.pageSize = size;
    }
  }
};
</script>

<style scoped>
.coupon-management {
  padding: 20px;
}

.dialog-footer {
  text-align: right;
}

.pagination-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

.page-size-select {
  margin-left: 10px;
  width: 150px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
